let express = require("express");
let path = require("path"); //系统路径模块
let fs = require("fs"); //文件模块
const router = express.Router();
const connect = require("../config/config.js");
const log4js  = require("../config/log.js");
let logger=log4js.getLogger("info")

//种族抽次数
router.post("/race", (req, res) => {
	let first_time = parseInt(req.body.first_time);
	let last_time = parseInt(req.body.last_time);
	let server_id = parseInt(req.body.server_id);
	let arr2 = [];let arr3 = [];let arr4 = [];
	let arr5 = [];let arr6 = [];let arr7 = [];
	let arr8 = [];let arr9 = [];let arr20 = [];
	let arr1 = [];let arr10 = [];let arr11 = [];
	let arr12 = [];let arr13 = [];let arr14 = [];
	let arr15 = [];let arr16 = [];let arr17 = [];
	let arr18 = [];let arr19 = [];let player_count1 = 0;
	let player_count2 = 0;let player_count3 = 0;
	let player_count15 = 0;let player_count9 = 0;
	let player_count4 = 0;let player_count5 = 0;
	let player_count6 = 0;let player_count7 = 0;
	let player_count8 = 0;let player_count10 = 0;
	let player_count11 = 0;let player_count12 = 0;
	let player_count13 = 0;let player_count14 = 0;
	let player_count16 = 0;let player_count17 = 0;
	let player_count18 = 0;let player_count19 = 0;
	let player_count20 = 0;let server_ids = 0;
	connect(function(err, db) {
		if (err) {
			logger.info("数据库连接失败");
		} else {
			db.collection("lottery").aggregate([{
					$match: {
						$and: [{
							date: {
								"$gte": first_time,
								"$lte": last_time
							}
						}, {
							server_id: server_id
						}]
					}
				},
				{
					$group: {
						_id: {
							lottery_id: "$lottery_id",
							uid: "$uid",
							server_id: "$server_id"
						},
						count: {
							$sum: 1
						}
					}
				},
			]).toArray(function(err, result) {
				if (err) {
					logger.info(err);;
					return;
				} else {
					if (result.length > 0) {
						result.forEach(item => {
							server_ids = item._id.server_id;
							if (0 <= item.count && item.count <= 10 && item._id.lottery_id === 7) {
								arr1.push(item._id.uid);
								player_count1 += item.count
							}else if (11 <= item.count && item.count <= 20 && item._id.lottery_id === 7) {
								arr2.push(item._id.uid);
								player_count2 += item.count
							}else if (21 <= item.count && item.count <= 50 && item._id.lottery_id === 7) {
								arr3.push(item._id.uid);
								player_count3 += item.count
							}else if (51 <= item.count && item.count <= 100 && item._id.lottery_id === 7) {
								arr4.push(item._id.uid);
								player_count4 += item.count;
							}else if (item.count > 100 && item._id.lottery_id === 7) {
								arr5.push(item._id.uid);
								player_count5 += item.count
							}
							if (1 <= item.count && item.count <= 10 && item._id.lottery_id === 10) {
								arr6.push(item._id.uid);
								player_count6 += item.count
							} else if (11 <= item.count && item.count <= 20 && item._id.lottery_id === 10) {
								arr7.push(item._id.uid);
								player_count7 += item.count;
							} else if (21 <= item.count && item.count <= 50 && item._id.lottery_id === 10) {
								arr8.push(item._id.uid);
								player_count8 += item.count;
							}else if (51 <= item.count && item.count <= 100 && item._id.lottery_id === 10) {
								arr9.push(item._id.uid);
								player_count9 += item.count
							} else if (item.count > 100 && item._id.lottery_id === 10) {
								arr10.push(item._id.uid);
								player_count10 += item.count
							}
							if (1 <= item.count && item.count <= 10 && item._id.lottery_id === 9) {
								arr11.push(item._id.uid);
								player_count11 += item.count;
							} else if (11 <= item.count && item.count <= 20 && item._id.lottery_id === 9) {
								arr12.push(item._id.uid);
								player_count12 += item.count
							} else if (21 <= item.count && item.count <= 50 && item._id.lottery_id === 9) {
								arr13.push(item._id.uid);
								player_count13 += item.count;
							} else if (51 <= item.count && item.count <= 100 && item._id.lottery_id === 9) {
								arr14.push(item._id.uid);
								player_count14 += item.count;
							} else if (item.count > 100 && item._id.lottery_id === 9) {
								arr15.push(item._id.uid);
								player_count15 += item.count
							}
							if (1 <= item.count && item.count <= 10 && item._id.lottery_id === 8) {
								arr16.push(item._id.uid);
								player_count16 += item.count
							} else if (11 <= item.count && item.count <= 20 && item._id.lottery_id === 8) {
								arr17.push(item._id.uid);
								player_count17 += item.count
							} else if (21 <= item.count && item.count <= 50 && item._id.lottery_id === 8) {
								arr18.push(item._id.uid);
								player_count18 += item.count
							} else if (51 <= item.count && item.count <= 100 && item._id.lottery_id === 8) {
								arr19.push(item._id.uid);
								player_count19 += item.count
							} else if (item.count > 100 && item._id.lottery_id === 8) {
								arr20.push(item._id.uid);
								player_count20 += item.count
							}
						});
						let obj1 = {
							race_draw_times: "[1-10]",
							water_frequency: player_count1,
							server_id: server_ids,
							water_population: arr1.length,
							number_of_fires: player_count6,
							number_of_fire_victims: arr6.length,
							wind_frequency: player_count11,
							wind_number: arr11.length,
							times: player_count16,
							number_of_people: arr16.length
						};
						let obj2 = {
							race_draw_times: "[11-20]",
							water_frequency: player_count2,
							server_id: server_ids,
							water_population: arr2.length,
							number_of_fires: player_count7,
							number_of_fire_victims: arr7.length,
							wind_frequency: player_count12,
							wind_number: arr12.length,
							times: player_count17,
							number_of_people: arr17.length
						};
						let obj3 = {
							race_draw_times: "[21-50]",
							water_frequency: player_count3,
							server_id: server_ids,
							water_population: arr3.length,
							number_of_fires: player_count8,
							number_of_fire_victims: arr8.length,
							wind_frequency: player_count3,
							wind_number: arr13.length,
							times: player_count18,
							number_of_people: arr18.length
						};
						let obj4 = {
							race_draw_times: "[51-100]",
							water_frequency: player_count4,
							server_id: server_ids,
							water_population: arr4.length,
							number_of_fires: player_count9,
							number_of_fire_victims: arr9.length,
							wind_frequency: player_count14,
							wind_number: arr14.length,
							times: player_count19,
							number_of_people: arr19.length
						};
						let obj5 = {
							race_draw_times: "[100+]",
							water_frequency: player_count5,
							water_population: arr5.length,
							server_id: server_ids,
							number_of_fires: player_count10,
							number_of_fire_victims: arr10.length,
							wind_frequency: player_count15,
							wind_number: arr15.length,
							times: player_count20,
							number_of_people: arr20.length
						};
						let arr = [];
						arr.push(obj1, obj2, obj3, obj4, obj5);
						res.send({
							code: 200,
							result: arr,
							msg: '获取成功'
						})
					}else{
						res.send({
							code:205,
							msg:'该服没有对应数据'
						})
					}
				}
			})
		}
	})
});

//礼包查询
router.post("/gift", (req, res) => {
	const first_time = parseInt(req.body.first_time);
	const last_time = parseInt(req.body.last_time);
	const server_id = parseInt(req.body.server_id);
	const gift_type = req.body.gift_type;
	let amounts = 0;
	let arr = [];
	let counts = 0;
	connect((err, db) => {
		if (err) {
			logger.info("数据库链接失败！")
		} else {
			if (gift_type === "周期付费") {
				db.collection("recharge").aggregate([{
						$match: {
							$and: [{
								date: {
									"$gte": first_time,
									"$lte": last_time
								}
							}, {
								recharge_id: {
									"$gte": 4,
									"$lte": 8,
									"$ne": 6
								}
							}, {
								server_id: server_id
							}],
						}
					},
					{
						$group: {
							_id: {
								recharge_id: "$recharge_id",
								dollar: "$dollar",
								server_id: "$server_id"
							},
							count: {
								$sum: 1
							},
						}
					}
				]).toArray(function(err, result) {
					if (err) {
						logger.info('error:' + err);
						return;
					} else {
						result.forEach((item) => {
							amounts += item._id.dollar * item.count;
							counts += item.count;
							let data = {
								recharge_id: item._id.recharge_id,
								dollar: parseFloat(item._id.dollar),
								gift_type: gift_type,
								server_id: item._id.server_id,
								amount: parseFloat((item._id.dollar * item.count).toFixed(2)),
								money_percentage: Math.round((item._id.dollar * item.count) / amounts * 10000) / (100.0).toFixed(2) +
									"%",
								count: item.count,
								times_percentage: Math.round((item.count / counts) * 10000) / (100.0).toFixed(2) + "%",
							};
							arr.push(data);
							arr.sort((a, b) => {
								return parseFloat(a.amount) < parseFloat(b.amount) ? 1 : -1
							});
						});
						res.send({
							code: 200,
							msg: "success",
							result: arr,
						})	
					}
				})
			} else if (gift_type === "常驻日周月礼包") {
				db.collection("recharge").aggregate([{
						$match: {
							$and: [{
								date: {
									"$gte": first_time,
									"$lte": last_time
								}
							}, {
								recharge_id: {
									"$gt": 10000,
									"$lt": 13000
								}
							}, {
								server_id: server_id
							}],
						}
					},
					{
						$group: {
							_id: {
								recharge_id: "$recharge_id",
								dollar: "$dollar",
								server_id: "$server_id"
							},
							count: {
								$sum: 1
							},
						}
					}
				]).toArray(function(err, result) {
					if (err) {
						logger.info('error:' + err);
						return;
					} else {
						result.forEach((item) => {
							amounts += item._id.dollar * item.count;
							counts += item.count;
							let data = {
								recharge_id: item._id.recharge_id,
								dollar: parseFloat(item._id.dollar),
								gift_type: gift_type,
								server_id: item._id.server_id,
								amount: parseFloat((item._id.dollar * item.count).toFixed(2)),
								money_percentage: Math.round((item._id.dollar * item.count) / amounts * 10000) / (100.0).toFixed(2) +
									"%",
								count: item.count,
								times_percentage: Math.round((item.count / counts) * 10000) / (100.0).toFixed(2) + "%",
							};
							arr.push(data);
							arr.sort((a, b) => {
								return parseFloat(a.amount) < parseFloat(b.amount) ? 1 : -1
							});
						});
						res.send({
							code: 200,
							msg: "success",
							result: arr,
						})	
					}
				})
			} else if (gift_type === "新手礼包") {
				db.collection("recharge").aggregate([{
						$match: {
							$and: [{
									date: {
										"$gte": first_time,
										"$lte": last_time
									}
								},
								{
									recharge_id: {
										"$in": [6, 9, 201, 202, 203, 204, 205, 206]
									}
								},
								{
									server_id: server_id
								}
							],
						}
					},
					{
						$group: {
							_id: {
								recharge_id: "$recharge_id",
								dollar: "$dollar",
								server_id: "$server_id"
							},
							count: {
								$sum: 1
							},
						}
					}
				]).toArray(function(err, result) {
					if (err) {
						logger.info('error:' + err);
						return;
					} else {
						result.forEach((item) => {
							amounts += item._id.dollar * item.count;
							counts += item.count;
							let data = {
								recharge_id: item._id.recharge_id,
								dollar: parseFloat(item._id.dollar),
								gift_type: gift_type,
								server_id: item._id.server_id,
								amount: parseFloat((item._id.dollar * item.count).toFixed(2)),
								money_percentage: Math.round((item._id.dollar * item.count) / amounts * 10000) / (100.0).toFixed(2) +
									"%",
								count: item.count,
								times_percentage: Math.round((item.count / counts) * 10000) / (100.0).toFixed(2) + "%",
							};
							arr.push(data);
							arr.sort((a, b) => {
								return parseFloat(a.amount) < parseFloat(b.amount) ? 1 : -1
							});
						});
						res.send({
							code: 200,
							msg: "success",
							result: arr,
						})	
					}
				})
			} else if (gift_type === "推送礼包") {
				db.collection("recharge").aggregate([{
						$match: {
							$and: [{
									date: {
										"$gte": first_time,
										"$lte": last_time
									}
								},
								{
									recharge_id: {
										"$gte": 13001
									}
								},
								{
									server_id: server_id
								}
							],
						}
					},
					{
						$group: {
							_id: {
								recharge_id: "$recharge_id",
								dollar: "$dollar",
								server_id: "$server_id"
							},
							count: {
								$sum: 1
							},
						}
					}
				]).toArray(function(err, result) {
					if (err) {
						logger.info('error:' + err);
						return;
					} else {
						result.forEach((item) => {
							amounts += item._id.dollar * item.count;
							counts += item.count;
							let data = {
								recharge_id: item._id.recharge_id,
								dollar: parseFloat(item._id.dollar),
								gift_type: gift_type,
								server_id: item._id.server_id,
								amount: parseFloat((item._id.dollar * item.count).toFixed(2)),
								money_percentage: Math.round((item._id.dollar * item.count) / amounts * 10000) / (100.0).toFixed(2) +
									"%",
								count: item.count,
								times_percentage: Math.round((item.count / counts) * 10000) / (100.0).toFixed(2) + "%",
							};
							arr.push(data);
							arr.sort((a, b) => {
								return parseFloat(a.amount) < parseFloat(b.amount) ? 1 : -1
							});
						});
						res.send({
							code: 200,
							msg: "success",
							result: arr,
						})	
					}
				})
			} else {
				return;
			}
		}
	})
});

//商城购买
router.post("/shopbuy", (req, res) => {
	let first_time = parseInt(req.body.first_time);
	let last_time = parseInt(req.body.last_time);
	let server_id = parseInt(req.body.server_id);
	let shop_id = parseInt(req.body.shop_id);
	let map1 = new Map();
	let map2 = new Map();
	let map3 = new Map();
	let arr = [];
	let money_consume = 0;
	let file = path.join(__dirname, '../map/goods.json'); //文件路径，__dirname为当前运行js文件的目录
	//读取json文件
	fs.readFile(file, 'utf8', function(err, data) {
		if (err) {
			res.send('文件读取失败');
		} else {
			let goods = JSON.parse(data);
			goods.forEach(item => {
				map1.set(item.product_id, item.wp_name);
				map3.set(item.product_id, item.name)
			});
			let files = path.join(__dirname, '../map/shopbuy.json'); 
			//读取json文件
			fs.readFile(files, 'utf8', function(err, data) {
				if (err) {
					res.send('文件读取失败');
				} else {
					let result = JSON.parse(data);
					result.forEach((item) => {
						map2.set(item.shop_id, item.name);
					});
					connect(function(err, db) {
						if (err) {
							logger.info("数据库链接失败！")
						} else {
							if (shop_id === 1001) {
								db.collection("shopbuy").aggregate([{
										$match: {
											$and: [{
													server_id: server_id
												},
												{
													shop_id: shop_id
												},
												{
													date: {
														"$gte": first_time,
														"$lte": last_time
													}
												}
											]
										}
									},
									{
										$group: {
											_id: {
												product_id: "$product_id",
												shop_id: "$shop_id",
												server_id: "$server_id",
												money_type: "$money_type",
												money_cost: "$money_cost",
												money_id: "$money_id"
											},
											count: {
												$sum: 1
											}
										}
									},
								]).toArray(function(err, results) {
									if (err) {
										logger.info('error:' + err);
										return;
									} else {
										results.forEach(item => {
											money_consume += item._id.money_cost;
											let obj = {
												product_name: map3.get(item._id.product_id),
												server_id: item._id.server_id,
												shop_type: map2.get(item._id.shop_id),
												money_type: item._id.money_type,
												money_cost: item._id.money_cost,
												gm_count: item.count,
												money_consume: (item._id.money_cost / money_consume).toFixed(7)
											};
											arr.push(obj);
											arr.sort((a, b) => {
												return a.gm_count < b.gm_count ? 1 : -1
											});
										});
										res.send({
											code: 200,
											msg: "success",
											result: arr,
										})	
									}
								})
							} else if (shop_id === 1002) {
								db.collection("shopbuy").aggregate([{
										$match: {
											$and: [{
													server_id: server_id
												},
												{
													shop_id: shop_id
												},
												{
													date: {
														"$gte": first_time,
														"$lte": last_time
													}
												}
											]
										}
									},
									{
										$group: {
											_id: {
												product_id: "$product_id",
												shop_id: "$shop_id",
												server_id: "$server_id",
												money_type: "$money_type",
												money_cost: "$money_cost",
												money_id: "$money_id"
											},
											count: {
												$sum: 1
											}
										}
									},
								]).toArray(function(err, results) {
									if (err) {
										logger.info('error:' + err);
										return;
									} else {
										results.forEach(item => {
											money_consume += item._id.money_cost;
											let obj = {
												product_name: map3.get(item._id.product_id),
												server_id: item._id.server_id,
												shop_type: map2.get(item._id.shop_id),
												money_type: item._id.money_type,
												money_cost: item._id.money_cost,
												gm_count: item.count,
												money_consume: (item._id.money_cost / money_consume).toFixed(7)
											};
											arr.push(obj);
											arr.sort((a, b) => {
												return a.gm_count < b.gm_count ? 1 : -1
											});
										});
										res.send({
											code: 200,
											msg: "success",
											result: arr,
										})	
									}
								})
							} else if (shop_id === 2001) {
								db.collection("shopbuy").aggregate([{
										$match: {
											$and: [{
													server_id: server_id
												},
												{
													shop_id: shop_id
												},
												{
													date: {
														"$gte": first_time,
														"$lte": last_time
													}
												}
											]
										}
									},
									{
										$group: {
											_id: {
												product_id: "$product_id",
												shop_id: "$shop_id",
												server_id: "$server_id",
												money_type: "$money_type",
												money_cost: "$money_cost",
												money_id: "$money_id"
											},
											count: {
												$sum: 1
											}
										}
									},
								]).toArray(function(err, results) {
									if (err) {
										logger.info('error:' + err);
										return;
									} else {
										results.forEach(item => {
											money_consume += item._id.money_cost;
											let obj = {
												product_name: map3.get(item._id.product_id),
												server_id: item._id.server_id,
												shop_type: map2.get(item._id.shop_id),
												money_type: item._id.money_type,
												money_cost: item._id.money_cost,
												gm_count: item.count,
												money_consume: (item._id.money_cost / money_consume).toFixed(7)
											};
											arr.push(obj);
											arr.sort((a, b) => {
												return a.gm_count < b.gm_count ? 1 : -1
											});
										});
										res.send({
											code: 200,
											msg: "success",
											result: arr,
										})	
									}
								})
							} else if (shop_id === 3001) {
								db.collection("shopbuy").aggregate([{
										$match: {
											$and: [{
													server_id: server_id
												},
												{
													shop_id: shop_id
												},
												{
													date: {
														"$gte": first_time,
														"$lte": last_time
													}
												}
											]
										}
									},
									{
										$group: {
											_id: {
												product_id: "$product_id",
												shop_id: "$shop_id",
												server_id: "$server_id",
												money_type: "$money_type",
												money_cost: "$money_cost",
												money_id: "$money_id"
											},
											count: {
												$sum: 1
											}
										}
									},
								]).toArray(function(err, results) {
									if (err) {
										logger.info('error:' + err);
										return;
									} else {
										results.forEach(item => {
											money_consume += item._id.money_cost;
											let obj = {
												product_name: map3.get(item._id.product_id),
												server_id: item._id.server_id,
												shop_type: map2.get(item._id.shop_id),
												money_type: item._id.money_type,
												money_cost: item._id.money_cost,
												gm_count: item.count,
												money_consume: (item._id.money_cost / money_consume).toFixed(7)
											};
											arr.push(obj);
											arr.sort((a, b) => {
												return a.gm_count < b.gm_count ? 1 : -1
											});
										});
										res.send({
											code: 200,
											msg: "success",
											result: arr,
										})	
									}
								})
							} else if (shop_id === 4001) {
								db.collection("shopbuy").aggregate([{
										$match: {
											$and: [{
													server_id: server_id
												},
												{
													shop_id: shop_id
												},
												{
													date: {
														"$gte": first_time,
														"$lte": last_time
													}
												}
											]
										}
									},
									{
										$group: {
											_id: {
												product_id: "$product_id",
												shop_id: "$shop_id",
												server_id: "$server_id",
												money_type: "$money_type",
												money_cost: "$money_cost",
												money_id: "$money_id"
											},
											count: {
												$sum: 1
											}
										}
									},
								]).toArray(function(err, results) {
									if (err) {
										logger.info('error:' + err);
										return;
									} else {
										results.forEach(item => {
											money_consume += item._id.money_cost;
											let obj = {
												product_name: map3.get(item._id.product_id),
												server_id: item._id.server_id,
												shop_type: map2.get(item._id.shop_id),
												money_type: item._id.money_type,
												money_cost: item._id.money_cost,
												gm_count: item.count,
												money_consume: (item._id.money_cost / money_consume).toFixed(7)
											};
											arr.push(obj);
											arr.sort((a, b) => {
												return a.gm_count < b.gm_count ? 1 : -1
											});
										});
										res.send({
											code: 200,
											msg: "success",
											result: arr,
										})	
									}
								})
							} else if (shop_id === 5001) {
								db.collection("shopbuy").aggregate([{
										$match: {
											$and: [{
												server_id: server_id
											}, {
												shop_id: shop_id
											}, {
												date: {
													"$gte": first_time,
													"$lte": last_time
												}
											}]
										}
									},
									{
										$group: {
											_id: {
												product_id: "$product_id",
												shop_id: "$shop_id",
												server_id: "$server_id",
												money_type: "$money_type",
												money_cost: "$money_cost",
												money_id: "$money_id"
											},
											count: {
												$sum: 1
											}
										}
									},
								]).toArray(function(err, results) {
									if (err) {
										logger.info('error:' + err);
										return;
									} else {
										results.forEach(item => {
											money_consume += item._id.money_cost;
											let obj = {
												product_name: map3.get(item._id.product_id),
												server_id: item._id.server_id,
												shop_type: map2.get(item._id.shop_id),
												money_type: item._id.money_type,
												money_cost: item._id.money_cost,
												gm_count: item.count,
												money_consume: (item._id.money_cost / money_consume).toFixed(7)
											};
											arr.push(obj);
											arr.sort((a, b) => {
												return a.gm_count < b.gm_count ? 1 : -1
											});
										});
										res.send({
											code: 200,
											msg: "success",
											result: arr,
										})	
									}
								})
							} else if (shop_id === 6001) {
								db.collection("shopbuy").aggregate([{
										$match: {
											$and: [{
												server_id: server_id
											}, {
												shop_id: shop_id
											}, {
												date: {
													"$gte": first_time,
													"$lte": last_time
												}
											}]
										}
									},
									{
										$group: {
											_id: {
												product_id: "$product_id",
												shop_id: "$shop_id",
												server_id: "$server_id",
												money_type: "$money_type",
												money_cost: "$money_cost",
												money_id: "$money_id"
											},
											count: {
												$sum: 1
											}
										}
									},
								]).toArray(function(err, results) {
									if (err) {
										logger.info('error:' + err);
										return;
									} else {
										results.forEach(item => {
											money_consume += item._id.money_cost;
											let obj = {
												product_name: map3.get(item._id.product_id),
												server_id: item._id.server_id,
												shop_type: map2.get(item._id.shop_id),
												money_type: item._id.money_type,
												money_cost: item._id.money_cost,
												gm_count: item.count,
												money_consume: (item._id.money_cost / money_consume).toFixed(7)
											};
											arr.push(obj);
											arr.sort((a, b) => {
												return a.gm_count < b.gm_count ? 1 : -1
											});
										});
										res.send({
											code: 200,
											msg: "success",
											result: arr,
										})	
									}
								})
							} else {
								return;
							}
						}
					})
				}
			});
		}
	});
});

//付费角色排行查询
router.post("/feeroleph", (req, res) => {
	let first_time = parseInt(req.body.first_time);
	let last_time = parseInt(req.body.last_time);
	let server_id = req.body.server_id;
	let arr = [];let obj = {};
	connect(function(err, db) {
		if (err) {
			logger.info("数据库连接失败");
		} else {
			db.collection("recharge").aggregate([{
					$match: {
						$and: [{
							$or:[{
								server_id: server_id
							}]
						}, {
							date: {
								"$gte": first_time,
								"$lte": last_time
							}
						}]
					}
				},
				{
					$lookup: {
						from: 'userinfo',
						localField: 'uid',
						foreignField: 'uid',
						as: 'ur'
					}
				},
				{
					$project: {
						_id: 0,
						"uid": 1,
						"dollar": 1,
						"server_id": 1,
						"ur.total_recharge": 1,
						"ur.uid": 1,
						"ur.nickname": 1,
						"ur.user_level": 1,
						"ur.vip_level": 1,
						"ur.reg_time": 1,
						"ur.login_time": 1,
						"ur.server_id": 1,
						"ur.afkcheckpoint": 1
					}
				},
			]).toArray(function(err, result) {
				if (err) {
					logger.info(err);
					return;
				} else {
					let newArr = [];
					result.forEach(el => {
						const result = newArr.findIndex(ol => {
							return el.uid === ol.uid;
						});
						if (result !== -1) {
							newArr[result].dollar = newArr[result].dollar + el.dollar;
						} else {
							newArr.push(el);
						}
					});
					newArr.forEach(item => {
						item.ur.forEach(items => {
							if (items.server_id === server_id) {
								obj = {
									dollar: parseFloat(item.dollar.toFixed(2)),
									total_recharge: parseFloat(items.total_recharge.toFixed(2)),
									server_id: item.server_id,
									uid: items.uid,
									nickname: items.nickname,
									user_level: items.user_level,
									vip_level: items.vip_level,
									reg_time: items.reg_time,
									login_time: items.login_time,
									checkpoint_id: items.afkcheckpoint,
								};
							} else {
								return;
							}
							arr.push(obj);
							arr.sort((a, b) => {
								return a.dollar < b.dollar ? 1 : -1;
							});
						})
					});
					res.send({
						code: 200,
						msg: "success",
						result: arr
					})
				}
			})
		}
	})
});

//抽卡统计
router.post("/lottery", (req, res) => {
	let first_time = parseInt(req.body.first_time);
	let last_time = parseInt(req.body.last_time);
	let server_id = parseInt(req.body.server_id);
	let server = 0;let arr1 = [];let arr2 = [];
	let arr3 = [];let arr4 = [];let arr5 = [];
	connect(function(err, db) {
		if (err) {
			logger.info("数据库链接失败！")
		} else {
			db.collection("lottery").aggregate([{
					$match: {
						$and: [{
							date: {
								"$gte": first_time,
								"$lte": last_time
							}
						}, {
							server_id: server_id
						}, {
							lottery_id: 2
						}],
					}
				},
				{
					$group: {
						_id: {
							uid: "$uid",
							server_id: "$server_id",
							lottery_num: "$lottery_num"
						},
						count: {
							$sum: 1
						}
					}
				}
			]).toArray(function(err, result) {
				if (err) {
					logger.info('error:' + err);
					return;
				} else {
					if (result.length > 0) {
						let newArr = [];
						result.forEach(item => {
							let obj = {
								uid: item._id.uid,
								server_id: item._id.server_id,
								lottery_num: item._id.lottery_num * item.count,
							};
							newArr.push(obj)
						});
						let map = new Map();
						//相同uid的数据去重，将lottery_num叠加起来获取抽卡的次数
						let keys = Array.from(new Set(newArr.map(item => item.uid)));
						newArr.forEach(item=>{
							map.set(item.uid,item.server_id);
						})
						var resArr = keys.map(key => {
						  return {      
							uid: key,      
							lottery_num: newArr.filter(item => item.uid == key).reduce((pre, cur) => pre+cur.lottery_num, 0),
							server_id:map.get(key)
						  }
						})
						resArr.forEach(item => {
							if (1 <= item.lottery_num && item.lottery_num <= 10) {
								arr1.push(item.uid);
							}
							if (11 <= item.lottery_num && item.lottery_num <= 20) {
								arr2.push(item.uid);
							}
							if (21 <= item.lottery_num && item.lottery_num <= 50) {
								arr3.push(item.uid);
							}
							if (51 <= item.lottery_num && item.lottery_num <= 100) {
								arr4.push(item.uid);
							}
							if (item.lottery_num > 100) {
								arr5.push(item.uid);
							}
							server = item.server_id;
						});
						let total_player = arr1.length + arr2.length + arr3.length + arr4.length + arr5.length;
						let obj1 = {
							server: server,
							section: "[1-10]",
							number_of_players: arr1.length,
							proportion_of_players: (Math.round((arr1.length / total_player * 10000))) / 100.00.toFixed(2) + '%'
						};
						let obj2 = {
							server: server,
							section: "[11-20]",
							number_of_players: arr2.length,
							proportion_of_players: (Math.round((arr2.length / total_player * 10000))) / 100.00.toFixed(2) + '%'
						};
						let obj3 = {
							server: server,
							section: "[21-50]",
							number_of_players: arr3.length,
							proportion_of_players: (Math.round((arr3.length / total_player * 10000))) / 100.00.toFixed(2) + '%'
						};
						let obj4 = {
							server: server,
							section: "[51-100]",
							number_of_players: arr4.length,
							proportion_of_players: (Math.round((arr4.length / total_player * 10000))) / 100.00.toFixed(2) + '%'
						};
						let obj5 = {
							server: server,
							section: "[100+]",
							number_of_players: arr5.length,
							proportion_of_players: (Math.round((arr5.length / total_player * 10000))) / 100.00.toFixed(2) + '%'
						};
						let arr = [];
						arr.push(obj1, obj2, obj3, obj4, obj5);
						res.send({
							code: 200,
							msg: "success",
							result: arr
						})
					}
				}
			})
		}
	})
});

//档位充值分布
router.post('/gearecharge', (req, res) => {
	let server_id = parseInt(req.body.server_id);
	let first_time = parseInt(req.body.first_time);
	let last_time = parseInt(req.body.last_time);
	let total_amount = 0;
	let number_of_peoples = 0;
	let total_recharge = 0;
	let arr = [];
	connect((err, db) => {
		if (err) {
			logger.info("数据库链接失败！")
		} else {
			db.collection("recharge").aggregate([{
					$match: {
						$and: [{
							date: {
								"$gte": first_time,
								"$lte": last_time
							}
						}, {
							server_id: server_id
						}]
					}
				},
				{
					$group: {
						_id: "$dollar",
						count: {
							$sum: 1
						}
					}
				},
				{
					$sort: {
						_id: 1
					}
				}
			]).toArray(function(err, result) {
				if (err) {
					logger.info('error:' + err);
					return;
				} else {
					result.forEach(item => {
						number_of_peoples += item.count;
						total_amount += item._id * item.count
					});
					result.forEach(item => {
						total_recharge += item._id * item.count;
						let obj = {
							server_id:server_id,
							dollar: parseFloat(item._id),
							sum_of_money: parseFloat((item._id * item.count).toFixed(2)),
							amount_proportion: (Math.round(((item._id * item.count) / total_amount * 10000))) / 100.00.toFixed(2) +
								'%',
							number_of_people: item.count,
							proportion_of_population: (Math.round((item.count / number_of_peoples * 10000))) / 100.00.toFixed(2) +
								'%'
						};
						arr.push(obj);
						arr.sort((a, b) => {
							return parseFloat(a.sum_of_money) < parseFloat(b.sum_of_money) ? 1 : -1
						})
					});
					res.send({
						code: 200,
						result: arr,
						msg: '获取成功',
						total_recharge: total_recharge
					})	
					
				}
			})
		}
	})
});
module.exports = router;
